package cn.quevo.ucenter.client.controller;

import cn.quevo.core.entity.ResponseResult;
import cn.quevo.core.entity.ResultEntity;
import cn.quevo.core.enums.ResultStatusEnum;
import cn.quevo.core.exception.CustomException;
import cn.quevo.core.lang.JSONUtils;
import cn.quevo.core.lang.StringUtils;
import cn.quevo.core.util.BeanUtils;
import cn.quevo.spring.SpringController;
import cn.quevo.spring.util.WebUtils;
import cn.quevo.ucenter.client.autoconfigure.UcenterProperties;
import cn.quevo.ucenter.client.entity.TokenResponse;
import cn.quevo.ucenter.client.entity.UserResponse;
import cn.quevo.ucenter.client.entity.query.UserQuery;
import cn.quevo.ucenter.client.service.ClientRestService;
import cn.quevo.ucenter.client.util.ClientUserUtil;
import cn.quevo.ucenter.core.OAuthContext;
import cn.quevo.ucenter.core.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author July july_sky@foxmail.com
 * @version 1.0
 * @date 2018/4/2 下午5:54
 * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
 */
@RestController("loginController")
public class LoginController extends SpringController {

    @Autowired
    private UcenterProperties ucenterProperties;
    @Resource(name = "clientRestService")
    private ClientRestService clientRestService;


    /**
     * 登录
     *
     * @param userQuery
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-04-02 18:35:11
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResultEntity login(UserQuery userQuery) throws CustomException {
        if (ucenterProperties.isTest()) {   // 验证是否测试,如果测试则无需验证验证码
            if (StringUtils.isBlank(userQuery.getUserName())) {
                userQuery.setUserName(ucenterProperties.getTestName());
            }
            if (StringUtils.isBlank(userQuery.getPassword())) {
                userQuery.setPassword(ucenterProperties.getTestPassword());
            }
//            ucenterProperties.setEnableImgCode(false);
        }
        if (StringUtils.isBlank(userQuery.getUserName()) || StringUtils.isBlank(userQuery.getPassword())) {
            return new ResultEntity(ResultStatusEnum.PARAMS_INCOMPLETE.getCode(), "用户名或密码不能为空");
        }
        if (ucenterProperties.isEnableImgCode()) {
            boolean imgCodePass = !StringUtils.isBlank(userQuery.getImgCode()) && !StringUtils.isBlank(WebUtils.getHttpSession("_captcha")) && userQuery.getImgCode().equalsIgnoreCase(WebUtils.getHttpSession("_captcha").toString());
            if (!imgCodePass) {
                return new ResultEntity(ResultStatusEnum.PARAMS_INCOMPLETE.getCode(), "验证码错误");
            }
        }
        TokenResponse tokenResponse = clientRestService.getTokenByPassword(userQuery.getUserName(), userQuery.getPassword());
        if (tokenResponse == null) {
            return new ResultEntity(ResultStatusEnum.ACCOUNT_PASSWORD_ERROR);
        }
        WebUtils.setSessionAndCookie(OAuthContext.OAUTH_TOKEN, tokenResponse.getAccessToken(), WebUtils.getHttpServletResponse());
        WebUtils.setHttpSession(OAuthContext.OAUTH_TOKEN,JSONUtils.format(tokenResponse));
        WebUtils.setHttpSession(OAuthContext.OAUTH_USER,JSONUtils.format(clientRestService.getUserByToken(tokenResponse.getAccessToken())));
        return ResponseResult.success(tokenResponse);
    }

    /**
     * 获取登录用户信息
     *
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-04-03 11:44:27
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public ResultEntity getLoginUser() throws CustomException {
        User user = ClientUserUtil.getLoginUser();
        if (user != null) {
            UserResponse userResponse = new UserResponse();
            BeanUtils.copyPropertiesExcludeNull(user, userResponse);
            return ResponseResult.success(userResponse);
        }
        return new ResultEntity(ResultStatusEnum.NOT_LOGIN);
    }
}
